package com.graduate.daydayup.view;

import android.content.Context;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.graduate.daydayup.R;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class PickerViewHelper {

    private TimePickerView pickerTime;

    public interface OnTimePickerListener {
        void onTimePicker(long timeStamp);
    }

    //选择任务截止时间
    public void showTimePicker(Context context, boolean[] type, int starYear, int endYear, OnTimePickerListener onTimePickerListener) {
        Date curDate = new Date(System.currentTimeMillis() + (60 * 60 * 24 * 1000));//获取当前日期
        SimpleDateFormat formater_year = new SimpleDateFormat("yyyy ", Locale.CHINA);
        formater_year.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String year_str = formater_year.format(curDate);
        int year_int = (int) Double.parseDouble(year_str);

        SimpleDateFormat formater_mouth = new SimpleDateFormat("MM ", Locale.CHINA);
        formater_mouth.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String mouth_str = formater_mouth.format(curDate);
        int mouth_int = (int) Double.parseDouble(mouth_str);

        SimpleDateFormat formater_day = new SimpleDateFormat("dd ", Locale.CHINA);
        formater_day.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String day_str = formater_day.format(curDate);
        int day_int = (int) Double.parseDouble(day_str);

        SimpleDateFormat formater_hour = new SimpleDateFormat("HH", Locale.CHINA);
        String hour_str = formater_hour.format(curDate);
        int hour_int = (int) Double.parseDouble(hour_str);

        SimpleDateFormat formater_minutes = new SimpleDateFormat("mm", Locale.CHINA);
        String minutes_str = formater_minutes.format(curDate);
        int minutes_int = (int) Double.parseDouble(minutes_str);
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(year_int - starYear, mouth_int - 1, day_int-1, hour_int, minutes_int);
        Calendar endDate = Calendar.getInstance();
        endDate.set(year_int + endYear, mouth_int - 1, day_int);
        if (pickerTime == null)
            pickerTime = new TimePickerBuilder(context, (date, v) -> {
                if (onTimePickerListener != null)
                    onTimePickerListener.onTimePicker(date.getTime());
            }).setType(type)//年月日时分
                    .setLabel("年", "月", "日", "时", "分", "秒")
                    .isCenterLabel(false)
                    .setDividerColor(context.getResources().getColor(R.color.color_d4d4))
                    .setContentTextSize(18)
                    .setDate(selectedDate)
                    .setLineSpacingMultiplier(1.2f)
                    .setTextXOffset(0, 0, 0, 0, 0, 0)
                    .setRangDate(startDate, endDate)
                    .setDecorView(null)
                    .build();
        pickerTime.show();
    }


}
